﻿using System;

namespace Lesson19_练习题
{
    #region 练习一
    //麻雀、鸵鸟、企鹅、鹦鹉、直升机、天鹅
    //直升机和部分鸟能飞
    //鸵鸟和企鹅不能飞
    //企鹅和天鹅能游泳
    //除直升机，其他都能走
    abstract class Animal
    {

    }
    interface IFly
    {
        public void Fly();
    }
    interface ISwim
    {
        public void Swim();
    }
    interface IWalk
    {
        public void Walk();
    }
    class Sparrow : Animal, IFly, IWalk
    {
        public void Fly()
        {
            Console.WriteLine("麻雀Fly");
        }

        public void Walk()
        {
            Console.WriteLine("麻雀Walk");
        }
    }
    class Ostrich : Animal, IWalk
    {
        public void Walk()
        {
            Console.WriteLine("鸵鸟Walk");
        }
    }
    class Penguin : Animal, IWalk, ISwim
    {
        public void Swim()
        {
            Console.WriteLine("企鹅Swim");
        }

        public void Walk()
        {
            Console.WriteLine("企鹅Walk");
        }
    }
    class Parrot : Animal, IFly, IWalk
    {
        public void Fly()
        {
            Console.WriteLine("鹦鹉Fly");
        }

        public void Walk()
        {
            Console.WriteLine("鹦鹉Walk");
        }
    }
    class Helicopter : IFly
    {
        public void Fly()
        {
            Console.WriteLine("直升机Fly");
        }
    }
    class Swan : Animal, IFly, ISwim, IWalk
    {
        public void Fly()
        {
            Console.WriteLine("天鹅Fly");
        }

        public void Swim()
        {
            Console.WriteLine("天鹅Swim");
        }

        public void Walk()
        {
            Console.WriteLine("天鹅Walk");
        }
    }
    #endregion

    #region 练习二
    //使用多态来模拟移动硬盘、U盘、MP3插到电脑上读取数据
    //移动硬盘与U盘都属于存储设备
    //MP3属于播放设备
    //但它们都能插在电脑上传输数据
    //电脑提供了一个USB接口
    //请实现电脑的传输数据的功能
    interface IUSB
    {
        void ReadData();
    }
    class StorageDevice : IUSB
    {
        public string name;
        public StorageDevice(string name)
        {
            this.name = name;
        }

        public void ReadData()
        {
            Console.WriteLine("{0}传输数据", name);
        }
    }

    class MP3 : IUSB
    {
        public void ReadData()
        {
            Console.WriteLine("MP3传输数据");
        }
    }
    class Computer
    {
        public IUSB usb1;
    }
    #endregion
    class Program
    {
        static void Main(string[] args)
        {
            StorageDevice hd = new StorageDevice("移动硬盘");
            StorageDevice ud = new StorageDevice("U盘");
            MP3 mp3 = new MP3();
            Computer computer = new Computer();
            //里氏替换原则 StorageDevice继承IUSB接口 IUSB装载StorageDevice和MP3
            computer.usb1 = hd;
            computer.usb1.ReadData();
            computer.usb1 = ud;
            computer.usb1.ReadData();
            computer.usb1 = mp3;
            computer.usb1.ReadData();
        }
    }
}